<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel common\models\searches\ProductSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '商品';
$this->params['breadcrumbs'][] = $this->title;
$category = \common\models\Category::find()->all();
?>
<div class="product-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('创建商品', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'category_id',
            [
                'attribute' => 'category_id',
                'value' => function ($model) {
                    return $model->category->name;
                },
                'filter' => Html::activeDropDownList(
                    $searchModel,
                    'category_id',
                    \yii\helpers\ArrayHelper::map($category, 'id', 'name'),
                    ['class' => 'form-control', 'prompt' => '请选择']
                ),
            ],
            'name',
            'sku',
            'stock',
            // 'weight',
            // 'market_price',
            // 'price',
            // 'brief',
            // 'content:ntext',
            // 'thumb',
            // 'image',
            // 'origin',
            // 'keywords',
            // 'description:ntext',
            //'type',
            [
                'attribute' => 'type',
                'value' => function ($model) {
                    return \common\models\Product::getType($model->type);
                }
            ],
            //'brand_id',
            [
                'attribute' => 'brand_id',
                'value' => function ($model) {
                    return $model->brand ? $model->brand->name : '-';
                },
                'filter' => Html::activeDropDownList(
                    $searchModel,
                    'brand_id',
                    \yii\helpers\ArrayHelper::map(\common\models\Brand::find()->all(), 'id', 'name'),
                    ['class' => 'form-control', 'prompt' => '请选择']

                )
            ],
            // 'star',
            // 'sales',
            //'status',
            [
                'attribute' => 'status',
                'format' => 'html',
                'value' => function ($model) {
                    if ($model->status == \common\models\Product::STATUS_ACTIVE) {
                        $class = 'label-success';
                    } elseif ($model->status == \common\models\Product::STATUS_INACTIVE) {
                        $class = 'label-warming';
                    } else {
                        $class = 'label-danger';
                    }
                    return '<span class="label ' . $class . '">' . \common\models\Product::getStatus($model->status) . '</span>';

                },
                'filter' => Html::activeDropDownList(
                    $searchModel,
                    'status',
                    \common\models\Product::getStatus(),
                    ['class' => 'form-control', 'prompt' => '请选择']

                )
            ],
            'created_at:date',
            // 'updated_at',
            // 'created_by',
            // 'updated_by',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
</div>
